package com.ruoyi.ai.service;

import com.ruoyi.ai.service.VectorService;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author coach tam
 * @email 327395128@qq.com
 * @values 坚持灵活 灵活坚持
 * @since 2025/3/24
 */
@Service
public class VectorServiceImpl implements VectorService {
    @Autowired
    @Qualifier("milvusVectorStore")
    private VectorStore vectorStore;
    @Override
    public List<String> searchQuestion(String question) {
        SearchRequest searchRequest = SearchRequest.builder().query(question).topK(1).build();
        List<Document> documents = vectorStore.similaritySearch(searchRequest);
        return  documents.stream().map(doc-> doc.getMetadata().getOrDefault("content", "").toString()).toList();
    }

}
